<?php

declare(strict_types=1);

namespace app\admin\controller;

use app\admin\controller\Base;
use think\Request;

class Filingcabinet extends Base
{
    protected $request;
    protected $service;
    protected $validate;

    protected $operator_id;
    public function __construct(Request $request, \app\admin\service\Filingcabinet $service)
    {
        $this->request = $request;
        $this->service = $service;
        $this->validate = new \app\admin\validate\Filingcabinet();

        $this->operator_id = $request->operator_id;
    }

    /**
     * @describe
     * @author: Bobo
     * @date: 2025-03-04 16:39:16
     * @param [type] @required
     * @return Renderable
     */
    public function  getFieldEnum()
    {
        $params = $this->request->params;
        $field = $params['field'] ?? 'all';
        $res = $this->service->getFieldEnum($field);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe 列表
     * @author: Bobo
     * @date: 2025-03-04 16:38:29
     * @param [type] @required
     * @return Renderable
     */
    public function index()
    {
        $params = empty($this->request->params) ? [] : $this->request->params;

        $pageIndex         = isset($params['pageIndex'])             ? intval($params['pageIndex'])             : 1;
        $pageSize          = isset($params['pageSize'])              ? intval($params['pageSize'])              : 1000;
        $orderId           = isset($params['order_id'])           ? trim($params['order_id']) : null;
        $customerId        = isset($params['customer_id'])           ? trim($params['customer_id']) : null;
        $fileType         = isset($params['file_type'])            ? intval($params['file_type']) : null;

        $where = [];
        if ($customerId) {
            $where[] = ['customer_id', 'in', $customerId];  // customer_id 在给定数组中
        }
        if ($orderId) {
            $where[] = ['order_id', 'in', $orderId];  // order_id 在给定数组中
        }
        if ($fileType) {
            $where['file_type'] = $fileType;  // file_type 等于指定值
        }


        $res =  $this->service->getList($pageIndex, $pageSize, $where);

        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe
     * @author: Bobo
     * @date: 2025-03-04 17:06:21
     * @param [type] @required
     * @return Renderable
     */
    public function create()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('create')->check($params)) {
            return $this->returnJsonData(100001, null, $this->validate->getError());
        }

        $params['create_time'] = date('Y-m-d H:i:s', time());
        $params['creator'] = $this->operator_id;


        $res  = $this->service->create($params);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe 删除文件柜记录
     * @author: jiang
     * @date: 2025-01-27
     * @return Renderable
     */
    public function delete()
    {
        $params = $this->request->params;
        $id = $params['id'] ?? null;

        if (!$id) {
            return $this->returnJsonData(100001, null, 'ID不能为空');
        }

        $res = $this->service->delete($id);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }
}
